package com.sc.nft.entity.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.entity.TradingPoolConfig;
import com.sc.nft.enums.TradingPoolStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

@Data
public class TradingPoolEasyConfigVO {
    @ApiModelProperty(value = "交易池配置")
    private Wrapper wrapper;

    @ApiModelProperty(value = "是否开启")
    private Boolean status;

    @ApiModelProperty(value = "交易池状态")
    private TradingPoolStatusEnum tradingPoolStatusEnum;


    // 内部类作为外层包装
    @Data
    public static class Wrapper {

        @ApiModelProperty(value = "唯一配置id")
        private Long id;

        @ApiModelProperty(value = "名称")
        private String name;

        @ApiModelProperty(value = "开始时间")

        @JsonFormat(pattern = "HH:mm:ss", timezone = "GMT+8")
        private Date startTime;

        @ApiModelProperty("结束时间")
        @JsonFormat(pattern = "HH:mm:ss", timezone = "GMT+8")
        private Date endTime;
    }
    public TradingPoolEasyConfigVO(TradingPoolConfig tradingPoolConfig) {
        if (BeanUtil.isNotEmpty(tradingPoolConfig)) {
            timeJudgment(tradingPoolConfig);
        }
        this.status= BeanUtil.isNotEmpty(tradingPoolConfig);
        if (this.status) {
            this.wrapper = BeanUtil.copyProperties(tradingPoolConfig, TradingPoolEasyConfigVO.Wrapper.class);
        }
    }

    /**
     * 时间的判断
     *
     * @param tradingPoolConfig
     */
    private void timeJudgment(TradingPoolConfig tradingPoolConfig) {
        // 根据时间推算,当前时间如果小于开始时间则是未开始 如果大于结束时间则是已结束,如果status=0的话则是关闭 如果在时间范围内则是正常
        // 转换后的当前时间
        DateTime localityTime = DateTime.of(LocalTime.now().truncatedTo(ChronoUnit.SECONDS).toString(), "HH:mm:ss");
        // 转换配置开始时间
        DateTime startTime = DateTime.of(tradingPoolConfig.getStartTime());
        // 转换配置结束时间
        DateTime endTime = DateTime.of(tradingPoolConfig.getEndTime());
        // 判断localityTime在不在startTime和endTime之间,在的话则是正常
        if (localityTime.isAfter(startTime) && localityTime.isBefore(endTime)) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.NORMAL;
        }

        // 如果当前时间小于开始时间
        if (localityTime.isBefore(startTime)) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.NOT_YET_STARTED;
        }

        // 如果当前时间大于结束时间
        if (localityTime.isAfter(endTime)) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.ALREADY_ENDED;
        }

        // 是否关闭
        if (!tradingPoolConfig.getStatus()) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.CLOSED;
        }
    }
}
